import { menuListToTree } from '@/utils/menuUtils';
import { request } from '@umijs/max';

/** 分页查询所有菜单 GET /system/menu/list */
export async function getMenuList(params?: API.QueryMenuPageParams) {
  return request<API.Resp<API.Menu[]>>('/system/menu/list', {
    method: 'GET',
    params,
  });
}

/** 新增菜单 POST /system/menu/add */
export async function addMenu(params: API.Menu) {
  return request<API.Resp>('/system/menu/add', {
    method: 'POST',
    data: params,
  });
}

/** 修改菜单 POST /system/menu/update */
export async function updateMenu(params: API.Menu) {
  return request<API.Resp>('/system/menu/update', {
    method: 'POST',
    data: params,
  });
}

/** 获取单个菜单 GET /system/menu/get/{id} */
export async function getMenuById(id: React.Key) {
  return request<API.Resp<API.Menu>>(`/system/menu/get/${id}`, {
    method: 'GET',
  });
}

/** 删除菜单 POST /system/menu/delete */
export async function deleteMenuById(id: React.Key) {
  return request<API.Resp>(`/system/menu/delete`, {
    method: 'DELETE',
    params: {
      id,
    },
  });
}

/** 获取单个菜单 GET /system/menu/tree/get */
export async function getMenuTree(): Promise<API.Resp<API.MenuTreeNode[]>> {
  const res = await getMenuList();
  return {
    ...res,
    data: res.data ? menuListToTree(res.data) : undefined,
  };
}
